package com.aistar.service.impl;

import com.aistar.mapper.ShopMapper;
import com.aistar.pojo.*;
import com.aistar.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private DishService dishService;

    @Autowired
    private DishImageService dishImageService;

    @Autowired
    private ReviewService reviewService;

    @Autowired
    private ReviewsImageService reviewsImageService;

    @Autowired
    private MemberService memberService;

    @Override
    public PageInfo<Shop> getAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Shop> shopList = shopMapper.selectByExample(null);

        return new PageInfo<Shop>(shopList);
    }

    @Override
    public ShopVO getById(Integer shopId) {
        // ShopVO 包含的信息（店铺信息，店铺中的所有菜品与菜品图片，点评信息，点评用户的信息）


        //1. 根据shop id查询店铺信息
        Shop shop = this.shopMapper.selectByPrimaryKey(shopId);


        //2. 根据shop id 查询店铺中的所有菜品(包含菜品头图)
        List<Dish> dishList = dishService.getByShopId(shopId);

        //3. 根据shop id 查询点评信息
        List<Review> reviewList = reviewService.getByShopId(shopId);

        List<ReviewsImage> reviewImageList = null;
        List<Member> reviewMemberList = new ArrayList<>();

        //Map<点评记录，点评用户>
        Map<Review, Member> reviewMemberMap = new HashMap<>();
        //4. 根据点评id 查询点评用户的信息
        for(Review review:reviewList){
            reviewImageList = reviewsImageService.getByReviewId(review.getReviewId());
           Member member = memberService.getById(review.getReviewMemberId());
           System.out.println("member:"+member);
            reviewMemberList.add(member);

            reviewMemberMap.put(review,member);

        }


        ShopVO shopVO = new ShopVO(shop,dishList,reviewMemberMap);


        return shopVO;
    }

    @Override
    public Shop getByIdOnlyShop(Integer id) {
        return null;
    }

    @Override
    public PageInfo<Shop> getBySearchWord(Shop shop,int pageNum, int pageSize) {


    System.out.println("service shop:"+shop);
        ShopExample shopExample =  new ShopExample();
        ShopExample.Criteria criteria = shopExample.createCriteria();

        if(shop.getShopName() !=null && !shop.getShopName().equals("")) {
            criteria.andShopNameLike("%"+shop.getShopName()+"%");  // and shop_name like ?

        }

        if(shop.getKeyword() !=null && !shop.getKeyword().equals(""))
            criteria.andKeywordLike("%"+shop.getKeyword()+"%");   // and keyword like ?

        if(shop.getShopAddArea() !=null && !shop.getShopAddArea().equals(""))
            criteria.andShopAddAreaLike("%"+shop.getShopAddArea()+"%");  // and shopAddArea like = ?

        PageHelper.startPage(pageNum,pageSize);
        List<Shop> shopList = this.shopMapper.selectByExample(shopExample);
        return new PageInfo<Shop>(shopList);
    }

    @Override
    public List<Shop> getByKeyWord(String keyword) {

        return  this.shopMapper.selectByKeyword(keyword);
    }
    public List<Shop> getByMoreKeyword(Shop record){
        return  this.shopMapper.selectByMoreKeyword(record);
    }

}
